package com.jingdun.sport.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingdun.sport.bean.model.NewSportRecords;
import com.jingdun.sport.bean.model.Room;
import com.jingdun.sport.bean.model.RoomUserRelation;
import com.jingdun.sport.bean.model.SportRecord;
import com.jingdun.sport.dao.NewSportRecordsMapper;
import com.jingdun.sport.dao.RoomUserRelationMapper;
import com.jingdun.sport.service.RoomUserRelationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
@AllArgsConstructor
public class RoomUserRelationServiceImpl extends ServiceImpl<RoomUserRelationMapper, RoomUserRelation> implements RoomUserRelationService {

    private RoomUserRelationMapper roomUserRelationMapper;

    public void addRoomUserRelation(RoomUserRelation roomUserRelation) {
        roomUserRelationMapper.addRoomUserRelation(roomUserRelation);
    }

    public Page<Map<String, Object>> getPaginationRoomUserRelation(@Param("params") Map<String, Object> m) {
        //新建分页
        Page<Map<String, Object>> page = new Page<Map<String, Object>>((long) m.get("current"), (long) m.get("size"));
        //返回结果
        page.setRecords(this.baseMapper.getPaginationRoomUserRelation(page, m));
        return page;
    }

    public List getRankFirstAndRankLastList(@Param("params") Map<String, Object> m) {
        //从数据库查询的数据
        List listDB = roomUserRelationMapper.getRankFirstAndRankLastList(m);

        //最终返回的数据
        List totalList = new ArrayList();

        //0为限时，1为限个数
        String fixedType = ((HashMap) listDB.get(0)).get("fixed_type").toString();
        List list = null;
        if (fixedType.equals("0")) {
            list = new ArrayList();
            String userId = ((HashMap) listDB.get(listDB.size() - 1)).get("user_id").toString();
            int totalMeasure = Integer.parseInt(((HashMap) listDB.get(listDB.size() - 1)).get("totalMeasure").toString());
            list.add("rankFirst");
            list.add(userId);
            list.add(totalMeasure);
            totalList.add(list);

            list = new ArrayList();
            userId = ((HashMap) listDB.get(0)).get("user_id").toString();
            totalMeasure = Integer.parseInt(((HashMap) listDB.get(0)).get("totalMeasure").toString());
            list.add("rankLast");
            list.add(userId);
            list.add(totalMeasure);
            totalList.add(list);
        } else if (fixedType.equals("1")) {
            list = new ArrayList();
            String userId = ((HashMap) listDB.get(0)).get("user_id").toString();
            int totalMeasure = Integer.parseInt(((HashMap) listDB.get(0)).get("totalMeasure").toString());
            list.add("rankFirst");
            list.add(userId);
            list.add(totalMeasure);
            totalList.add(list);

            list = new ArrayList();
            userId = ((HashMap) listDB.get(listDB.size() - 1)).get("user_id").toString();
            totalMeasure = Integer.parseInt(((HashMap) listDB.get(listDB.size() - 1)).get("totalMeasure").toString());
            list.add("rankLast");
            list.add(userId);
            list.add(totalMeasure);
            totalList.add(list);
        }
        return totalList;
    }

    public Page<Map<String, Object>> getPaginationAllPeopleSport(@Param("params") Map<String, Object> m) {
        //新建分页
        Page<Map<String, Object>> page = new Page<Map<String, Object>>((long) m.get("current"), (long) m.get("size"));
        //返回结果
        page.setRecords(this.baseMapper.getPaginationAllPeopleSport(page, m));
        return page;
    }

    public List<RoomUserRelation> getRoomUserRelationAllDoneList(@Param("params") Map<String, Object> m) {
        return roomUserRelationMapper.getRoomUserRelationAllDoneList(m);
    }

    public RoomUserRelation getRoomUserRelationDetail(@Param("params") Map<String, Object> m) {
        return roomUserRelationMapper.getRoomUserRelationDetail(m);
    }

    public void updateRoomUserRelation(RoomUserRelation roomUserRelation) {
        roomUserRelationMapper.updateRoomUserRelation(roomUserRelation);
    }

    @Override
    public List<RoomUserRelation> getAllRoomMapUserByRoomId(Set<Long> ufinishedRoomIdSet) {
        return roomUserRelationMapper.getAllRoomMapUserByRoomId(ufinishedRoomIdSet);
    }

    @Override
    public List<RoomUserRelation> getPageRoomMapByUserId(Map m) {
        return roomUserRelationMapper.getPageRoomMapByUserId(m);
    }

    @Override
    public void updateAllDoneByUserIdAndRoomId(Map m) {
        roomUserRelationMapper.updateAllDoneByUserIdAndRoomId(m);
    }

    @Override
    public void updateTakenPriceByUserIdAndRoomId(Map map) {
        roomUserRelationMapper.updateTakenPriceByUserIdAndRoomId(map);
    }

    @Override
    public long getCountRoomMapByUserId(Map m) {
        return roomUserRelationMapper.getCountRoomMapByUserId(m);
    }

    @Override
    public boolean getByRoomIdAndUserId(String roomId, String userId) {
        return roomUserRelationMapper.getByRoomIdAndUserId(roomId, userId) > 0;
    }

    @Autowired
    private NewSportRecordsMapper newSportRecordsMapper;
    
    @Override
    public NewSportRecords userNewSportProjectBy(Long sportProject, String userId) {
        return newSportRecordsMapper.userNewSportProjectBy(sportProject, userId);
    }
}
